import { StageTypeEnum } from '../constant/enums';

// 根据比赛类型和 计划人数，计算实际要生成的e2p数量
export const getE2PCount = (
  type: StageTypeEnum,
  participantCount: number,
  groupCount?: number,
): number => {
  if (type === StageTypeEnum.Loop) {
    if (!groupCount) {
      throw new Error('Group count is required for round-robin competition');
    }

    const playersPerGroup = Math.ceil(participantCount / groupCount);
    const evenPlayersPerGroup =
      playersPerGroup % 2 === 0 ? playersPerGroup : playersPerGroup + 1;

    return evenPlayersPerGroup * groupCount;
  } else if (type === StageTypeEnum.Elimination) {
    let drawPositions = 1;
    while (drawPositions < participantCount) {
      drawPositions *= 2;
    }

    return drawPositions;
  } else {
    throw new Error('Invalid competition type');
  }
};
